﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Functions;
using SchoolProject2012.Areas.Eleve.Models;
using System.Web.Security;

namespace SchoolProject2012.Areas.Eleve.Controllers
{
    public class ActuController : Controller
    {
        private EleveSchoolProjectBddEntities db = new EleveSchoolProjectBddEntities();
        //
        // GET: /Eleve/Actu/
        //[Authorize(Roles = "Admin, Eleve, BDE, Delegue")]
        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            string login = Session["login"].ToString();
            var news = db.VOIR.Where(x => x.LOGINPERS == login);
            return View(news);
        }

        //
        // GET: /Eleve/Actu/Details/5
        //[Authorize(Roles = "Admin, Eleve, BDE, Delegue")]
        public ActionResult Details(string langue, int id)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            var news = db.ACTUALITE.Single(x => x.ID_ACTU == id);
            return View(news);
        }

        //
        // GET: /Eleve/Actu/Create
        //[Authorize(Roles = "Admin, BDE, Delegue")]
        public ActionResult Create(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            MultiSelectList classe = new MultiSelectList(db.CLASSE.ToList(),"LIBCLASSE","LIBCLASSE");
            ViewData["classe"] = classe;
            return View();
        }

        //
        // POST: /Eleve/Actu/Create

        [HttpPost]
        //[Authorize(Roles = "Admin, BDE, Delegue")]
        public ActionResult Create(string langue,ACTUALITE actuToSave, FormCollection collection)
        {
            try
            {
                actuToSave.LOGINPERS = Session["login"].ToString();
                actuToSave.DATE_ACTU = DateTime.Now;
                db.ACTUALITE.AddObject(actuToSave);
                string login = Session["login"].ToString();
                string cl = Utils.getCurrentYearForClass();
                int idclasse = db.ELEVE.SingleOrDefault(x => x.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == cl).IDCLASS;
                if (Roles.IsUserInRole("Delegue"))
                {
                    var classe = db.ELEVECLASS.Where(x => x.CLASSE.IDCLASSE == idclasse).ToList();
                    foreach (ELEVECLASS e in classe)
                    {
                        VOIR v = new VOIR();
                        v.LU = false;
                        v.LOGINPERS = e.LOGINPERS;
                        v.ID_ACTU = actuToSave.ID_ACTU;
                        db.VOIR.AddObject(v);
                    }
                }
                if (Roles.IsUserInRole("BDE"))
                {
                    var classe = db.ELEVECLASS.Where(x => x.CLASSE.IDCLASSE == idclasse).ToList();
                    foreach (PersonneView p in db.PersonneView.ToList())
                    {
                        VOIR v = new VOIR();
                        v.LU = false;
                        v.LOGINPERS = p.LOGIN;
                        v.ID_ACTU = actuToSave.ID_ACTU;
                        db.VOIR.AddObject(v);
                    }
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Eleve/Actu/Edit/5
        //[Authorize(Roles = "Admin, BDE, Delegue")]
        public ActionResult Edit(int id, string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            return View();
        }

        //
        // POST: /Eleve/Actu/Edit/5

        [HttpPost]
        //[Authorize(Roles = "Admin, BDE, Delegue")]
        public ActionResult Edit(int id, FormCollection collection, string langue)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Eleve/Actu/Delete/5
        //[Authorize(Roles = "Admin, BDE, Delegue")]
        public ActionResult Delete(int id, string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            return View();
        }

        //
        // POST: /Eleve/Actu/Delete/5

        [HttpPost]
        [Authorize(Roles = "Admin, BDE, Delegue")]
        public ActionResult Delete(int id, FormCollection collection, string langue)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
